
#include <config.h>
#include <encoding_manager.h>
#include <string.h>

static int Utf16leIsSupport(unsigned char *pucBufferHead);
static int Utf16leGetCodeFrmBuffer(unsigned char *pucBufferStart, unsigned char *pucBufferEnd, unsigned int *pdwCode);

static T_EncodingOpr g_tUtf16leEncodingOpr = {
	.name             = "utf-16le",
	.iHead            = 2,
	.IsSupport        = Utf16leIsSupport,
	.GetCodeFrmBuffer = Utf16leGetCodeFrmBuffer,
};

static int Utf16leIsSupport(unsigned char *pucBufferHead)
{
	const char aUtf16leHead[] = {0xFF, 0xFE, 0};

	if(strncmp(aUtf16leHead, (const char *)pucBufferHead, 2) == 0)
		return 1;
	else
		return 0;
}

static int Utf16leGetCodeFrmBuffer(unsigned char *pucBufferStart, unsigned char *pucBufferEnd, unsigned int *pdwCode)
{
	if(pucBufferStart + 1 < pucBufferEnd)
	{
		*pdwCode = ((unsigned int)(pucBufferStart[1]<<8)) + pucBufferStart[0];
		return 2;
	}
	else
	{
		return 0;
	}
}

int Utf16leEncodingRegister(void)
{
	AddFontOprForEncoding(&g_tUtf16leEncodingOpr, GetFontOpr("freetype"));
	AddFontOprForEncoding(&g_tUtf16leEncodingOpr, GetFontOpr("ascii"));	
	return RegisterEncodingOpr(&g_tUtf16leEncodingOpr);
}

